<?php
// $Id$

/**
 * path_init()
 * 
 * Initializes the desired path for display to the user, 
 * and includes any necessary files
 * 
 * @return
 */
function path_init(&$vars, $run_tests = FALSE)
{
	global $q;
	if (empty($q))
	{
		$q = 'index.php';
	}
	
	$path_info = pathinfo($q); 
	if (!$run_tests)
	{
		if (!empty($q))
		{	
			if (!isset($path_info['extension']))
			{
				if (file_exists('./' . $q . '.php'))
				{
					$q = $q . '.php';
				}
				else 
				{
					$q = substr($path_info['dirname'] . '/index.php', 1);
				}
			}
			
			path_load($vars, $q);
		}
		else
		{
			path_load_front($vars);
		}
	}
	else 
	{
		global $test;
		$test = $_GET['test'];
		
		$q = (strlen($path_info['dirname']) > 1 ? $path_info['dirname'] . '/' : '') . 'tests/';
		if (!isset($path_info['extension']))
		{
			$q .= 'index.php'; 
		}
		else
		{
			$q .= $path_info['basename'];
		}
		
		// should I change $q here too?
		if (file_exists('.' . $q))
		{
			path_load($vars, substr($q, 1));
		}
		else
		{
			path_load_front($vars, TRUE);
		}
	}
	
	$vars['q'] = $q;
}

/**
 * path_load()
 * 
 * This function does the actual including of necessary files
 * 
 * @param mixed $file
 * @return
 */
function path_load(&$vars, $file)
{
	if (file_exists('./' . $file))
	{
		include_once ('./' . $file);
		
		if (!isset($main))
		{
			path_load_front($vars);
			return;
		}
		
		if (function_exists($main))
		{
			global $load;
			$load = $main;
		}
		else
		{
			path_load_front($vars);
		}
	}
}

/**
 * path_load_front()
 * 
 * This function loads the home page, in case of any bad URLs or system error
 * 
 * @return
 */
function path_load_front(&$vars, $run_tests = FALSE)
{
	global $load;

	if (!$run_tests)
	{
		include_once ('./front.php');
	}
	else
	{
		include_once ('./tests/index.php');
	}

	if (function_exists($main))
	{
		$load = $main;
	}
	
	$vars['load'] = $load;
}

/**
 * path_menu_links()
 * 
 * Returns an array of menu links for the navigation bar
 * 
 * @return
 */
function path_menu_links()
{
	return array(
		'developer' => array(
			'#anchor' => 'Developers',
			'#attributes' => array(
				'class' => 'ui-navigation-item'
			),
			'#href' => base_path(),
			'#title' => 'View, add, and edit developers'
		),
		'feature' => array(
			'#anchor' => 'Features',
			'#attributes' => array(
				'class' => 'ui-navigation-item'
			),
			'#href' => 'feature',
			'#title' => 'View, add, and edit features'
		),
		'milestone' => array(
			'#anchor' => 'Milestones',
			'#attributes' => array(
				'class' => 'ui-navigation-item'
			),
			'#href' => 'milestone',
			'#title' => 'View, add, and edit milestones'
		),
		'task' => array(
			'#anchor' => 'Tasks',
			'#attributes' => array(
				'class' => 'ui-navigation-item'
			),
			'#href' => 'task',
			'#title' => 'View, add, and edit tasks'
		)
	);
}

/**
 * render_menu()
 * 
 * Renders those menu links
 * 
 * @param mixed $menu
 * @return
 */
function render_menu($menu)
{
	return render_links($menu, array('class' => 'ui-navigation menu'));
}

/**
 * render_links()
 * 
 * Generally renders and links in a list
 * 
 * @param mixed $links
 * @param mixed $attributes
 * @return
 */
function render_links($links, $attributes = array('class' => 'links'))
{
	global $q;
	
	$output = '';
	$count = count($links);
	if ($count > 0)
	{
		$output = '<ul' . render_attributes($attributes) . '>' . "\n";
		
		$i = 0;
		foreach ($links as $key => $link) 
		{
			$classes = array('link-item');
			
			$classes[] = ($i + 1) % 2 == 0 ? 'even' : 'odd';
			
			if ($i == 0)
			{
				$classes[] = 'first';
			}
			else if ($i == $count - 1)
			{
				$classes[] = 'last';
			}
	
			if (isset($link['#href']) && $link['#href'] == $q)
			{
				$class[] = 'active';
			}
			
			$output .= '<li class="' . implode(' ', $classes) . '">' . a($link) . '</li>' . "\n";
			$i++;
		}
		
		$output .= '</ul>' . "\n";
	}
	
	return $output;
}
